﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model.Utils
{
    class MessageUtil
    {
        public static bool Send4Forgotten(string phone, string message, int timeoutMinute, out string retMsg)
        {
            bool isSuccess = true;

            CCPRestSDK.CCPRestSDK api = new CCPRestSDK.CCPRestSDK();
            //ip格式如下，不带https://
            //bool isInit = api.init("sandboxapp.cloopen.com", "8883");
            //api.setAccount("aaf98f89493ff1d301494b23d40a064e", "5825ce8126054f4f837b1a8329d66065");
            //api.setAppId("aaf98f89493ff1d301494b23f12d0651");
            bool isInit = api.init(Configs.MessageAddress, Configs.MessagePort);
            api.setAccount(Configs.MessageAccount, Configs.MessageToken);
            api.setAppId(Configs.MessageAppId);
            try
            {
                if (isInit)
                {
                    string[] msg = { message, timeoutMinute.ToString() };
                    Dictionary<string, object> retData = api.SendTemplateSMS(phone, Configs.MessageForgottenTemplateId, msg);
                    retMsg = getDictionaryData(retData);
                }
                else
                {
                    retMsg = "初始化失败";
                    isSuccess = false;
                }
            }
            catch (Exception exc)
            {
                JizSport.Common.Logger.ErrorToTag("MessageUtil", "发送短信失败,phone:{0},message:{1},ex:{2}",
                    phone,
                    message,
                    exc);
                retMsg = exc.Message;
                isSuccess = false;
            }
            finally
            {
            }
            return isSuccess;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phone">电话</param>
        /// <param name="message">短信内容</param>
        /// <param name="timeoutMinute"></param>
        /// <param name="retMsg"></param>
        /// <returns></returns>
        public static bool Send(string phone, string message, int timeoutMinute, out string retMsg)
        {
            bool isSuccess = true;

            CCPRestSDK.CCPRestSDK api = new CCPRestSDK.CCPRestSDK();
            //ip格式如下，不带https://
            //bool isInit = api.init("sandboxapp.cloopen.com", "8883");
            //api.setAccount("aaf98f89493ff1d301494b23d40a064e", "5825ce8126054f4f837b1a8329d66065");
            //api.setAppId("aaf98f89493ff1d301494b23f12d0651");
            bool isInit = api.init(Configs.MessageAddress, Configs.MessagePort);
            api.setAccount(Configs.MessageAccount, Configs.MessageToken);
            api.setAppId(Configs.MessageAppId);
            try
            {
                if (isInit)
                {
                    string[] msg = { message, timeoutMinute.ToString() };
                    Dictionary<string, object> retData = api.SendTemplateSMS(phone, Configs.MessageRegisterTemplateId, msg);
                    retMsg = getDictionaryData(retData);
                }
                else
                {
                    retMsg = "初始化失败";
                    isSuccess = false;
                }
            }
            catch (Exception exc)
            {
                JizSport.Common.Logger.ErrorToTag("MessageUtil", "发送短信失败,phone:{0},message:{1},ex:{2}",
                    phone,
                    message,
                    exc);
                retMsg = exc.Message;
                isSuccess = false;
            }
            finally
            {
            }
            return isSuccess;
        }

        private static string getDictionaryData(Dictionary<string, object> data)
        {
            string ret = null;
            foreach (KeyValuePair<string, object> item in data)
            {
                if (item.Value != null && item.Value.GetType() == typeof(Dictionary<string, object>))
                {
                    ret += item.Key.ToString() + "={";
                    ret += getDictionaryData((Dictionary<string, object>)item.Value);
                    ret += "};";
                }
                else
                {
                    ret += item.Key.ToString() + "=" + (item.Value == null ? "null" : item.Value.ToString()) + ";";
                }
            }
            return ret;
        }
    }
}
